package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.AppConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.CommonBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.message.presenter.EditGroupInfoPresenter;
import com.beiing.tianshuai.tianshuai.message.view.EditGroupInfoViewImpl;
import com.beiing.tianshuai.tianshuai.util.SPUtils;
import com.beiing.tianshuai.tianshuai.widget.CustomProgressDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

public class EditGroupNameActivity extends BaseActivity implements EditGroupInfoViewImpl {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.et_new_group_name)
    EditText mEtNewGroupName;
    @BindView(R.id.toolbar_btn_publish)
    TextView mToolbarBtnPublish;

    private Unbinder                    mUnbinder;
    private Context                     mContext;
    private String                      mGroupId;
    private String                      mGroupNotice;
    private String                      mGroupDesc;
    private String                      mGId;
    private EditGroupInfoPresenter      mPresenter;
    private String                      mGroupName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_group_name;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        initData();
        initToolbar();
        initListener();
        initPresenter();
    }

    private void initData() {
        Intent intent = getIntent();
        mGroupName = intent.getStringExtra("groupName");
        mGroupNotice = intent.getStringExtra("groupNotice");
        mGroupDesc = intent.getStringExtra("groupDesc");
        mGroupId = intent.getStringExtra("groupId");
        mGId = intent.getStringExtra("gId");
        mEtNewGroupName.setHint(mGroupName);
    }

    private void initToolbar() {
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarBtnPublish.setVisibility(View.VISIBLE);
        mToolbarTvTitle.setText("群聊名称");
        mToolbarBtnPublish.setText("保存");
    }

    private void initPresenter() {
        mPresenter = new EditGroupInfoPresenter(this);
    }

    private void initListener() {
        //保存
        mToolbarBtnPublish.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                mPresenter.getEditGroupInfoRequestResponse(
                        UserInfoBean.getUid(mContext),
                        mGId,
                        mGroupId,
                        mEtNewGroupName.getText().toString(),
                        mGroupDesc,
                        mGroupNotice
                );
            }
        });

        //返回
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    @Override
    public void showProgress() {
        CustomProgressDialog.showLoading(mContext, "正在提交，请稍后...", true);
    }

    @Override
    public void hideProgress() {
        CustomProgressDialog.stopLoading();
    }

    @Override
    public void onRequestSuccess(CommonBean bean) {
        if (bean.getCode() == 200) {
            SPUtils.put(mContext, "nickname" + mGroupId, mEtNewGroupName.getText().toString());
            Intent intent = new Intent();
            intent.putExtra("groupName", mEtNewGroupName.getText().toString());
            this.setResult(AppConstant.EDIT_GROUP_NAME, intent);
            this.finish();
        } else {
            showShortText("哎呀，请求出错了...");
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        showShortText("网络异常");
    }
}
